home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / f2c_5_92 Folder / f2c_5_92 / libI77 / err.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-26  |  5.2 KB  |  226 lines  |  [TEXT/ttxt]

  1. #ifndef THINK_C
  2. #include "sys/types.h"
  3. #ifndef MSDOS
  4. #include "sys/stat.h"
  5. #endif
  6. #endif
  7. #include "f2c.h"
  8. #include "fio.h"
  9. #include "fcntl.h"
  10. #ifndef O_WRONLY
  11. #define O_WRONLY 1
  12. #endif
  13.  
  14. extern FILE *fdopen();
  15.  
  16. /*global definitions*/
  17. unit units[MXUNIT];    /*unit table*/
  18. flag init;    /*0 on entry, 1 after initializations*/
  19. cilist *elist;    /*active external io list*/
  20. flag reading;    /*1 if reading, 0 if writing*/
  21. flag cplus,cblank;
  22. char *fmtbuf;
  23. flag external;    /*1 if external io, 0 if internal */
  24. int (*doed)(),(*doned)();
  25. int (*doend)(),(*donewrec)(),(*dorevert)();
  26. flag sequential;    /*1 if sequential io, 0 if direct*/
  27. flag formatted;    /*1 if formatted io, 0 if unformatted*/
  28. int (*getn)(),(*putn)();    /*for formatted io*/
  29. FILE *cf;    /*current file*/
  30. unit *curunit;    /*current unit*/
  31. int recpos;    /*place in current record*/
  32. int cursor,scale;
  33.  
  34. /*error messages*/
  35. char *F_err[] =
  36. {
  37.     "error in format",                /* 100 */
  38.     "illegal unit number",                /* 101 */
  39.     "formatted io not allowed",            /* 102 */
  40.     "unformatted io not allowed",            /* 103 */
  41.     "direct io not allowed",            /* 104 */
  42.     "sequential io not allowed",            /* 105 */
  43.     "can't backspace file",                /* 106 */
  44.     "null file name",                /* 107 */
  45.     "can't stat file",                /* 108 */
  46.     "unit not connected",                /* 109 */
  47.     "off end of record",                /* 110 */
  48.     "truncation failed in endfile",            /* 111 */
  49.     "incomprehensible list input",            /* 112 */
  50.     "out of free space",                /* 113 */
  51.     "unit not connected",                /* 114 */
  52.     "read unexpected character",            /* 115 */
  53.     "bad logical input field",            /* 116 */
  54.     "bad variable type",                /* 117 */
  55.     "bad namelist name",                /* 118 */
  56.     "variable not in namelist",            /* 119 */
  57.     "no end record",                /* 120 */
  58.     "variable count incorrect",            /* 121 */
  59.     "subscript for scalar variable",        /* 122 */
  60.     "invalid array section",            /* 123 */
  61.     "substring out of bounds",            /* 124 */
  62.     "subscript out of bounds",            /* 125 */
  63.     "can't read file",                /* 126 */
  64.     "can't write file",                /* 127 */
  65.     "'new' file exists"                /* 128 */
  66. };
  67. #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
  68. fatal(n,s) char *s;
  69. {
  70.     if(n<100 && n>=0) perror(s); /*SYSDEP*/
  71.     else if(n >= (int)MAXERR || n < -1)
  72.     {    fprintf(stderr,"%s: illegal error number %d\n",s,n);
  73.     }
  74.     else if(n == -1) fprintf(stderr,"%s: end of file\n",s);
  75.     else
  76.         fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
  77.     if (curunit) {
  78.         fprintf(stderr,"apparent state: unit %d ",curunit-units);
  79.         fprintf(stderr, curunit->ufnm ? "named %s\n" : "(unnamed)\n",
  80.             curunit->ufnm);
  81.         }
  82.     else
  83.         fprintf(stderr,"apparent state: internal I/O\n");
  84.     if (fmtbuf)
  85.         fprintf(stderr,"last format: %s\n",fmtbuf);
  86.     fprintf(stderr,"lately %s %s %s %s",reading?"reading":"writing",
  87.         sequential?"sequential":"direct",formatted?"formatted":"unformatted",
  88.         external?"external":"internal");
  89.     sig_die(" IO", 1);
  90. }
  91. /*initialization routine*/
  92. f_init()
  93. {    unit *p;
  94.  
  95.     init=1;
  96.     p= &units[0];
  97.     p->ufd=stderr;
  98.     p->useek=canseek(stderr);
  99. #ifdef COMMENTED_OUT
  100.     if(isatty(fileno(stderr))) {
  101.         extern char *malloc();
  102.         setbuf(stderr, malloc(BUFSIZ));
  103.         /* setvbuf(stderr, _IOLBF, 0, 0); */
  104.     }    /* wastes space, but win for debugging in windows */
  105. #endif
  106. #ifdef NON_UNIX_STDIO
  107.     {extern char *malloc(); setbuf(stderr, malloc(BUFSIZ));}
  108. #else
  109.     stderr->_flag &= ~_IONBF;
  110. #endif
  111.     p->ufmt=1;
  112.     p->uwrt=1;
  113.     p = &units[5];
  114.     p->ufd=stdin;
  115.     p->useek=canseek(stdin);
  116.     p->ufmt=1;
  117.     p->uwrt=0;
  118.     p= &units[6];
  119.     p->ufd=stdout;
  120.     p->useek=canseek(stdout);
  121.     /* IOLBUF and setvbuf only in system 5+ */
  122. #ifdef COMMENTED_OUT
  123.     if(isatty(fileno(stdout))) {
  124.         extern char _sobuf[];
  125.         setbuf(stdout, _sobuf);
  126.         /* setvbuf(stdout, _IOLBF, 0, 0);    /* the buf arg in setvbuf? */
  127.         p->useek = 1;    /* only within a record no bigger than BUFSIZ */
  128.     }
  129. #endif
  130.     p->ufmt=1;
  131.     p->uwrt=1;
  132. }
  133. canseek(f) FILE *f; /*SYSDEP*/
  134. {
  135. #ifdef MSDOS
  136.     return !isatty(fileno(f));
  137. #else
  138.     struct stat x;
  139.  
  140.     if (fstat(fileno(f),&x) < 0)
  141.         return(0);
  142. #ifdef S_IFMT
  143.     switch(x.st_mode & S_IFMT) {
  144.     case S_IFDIR:
  145.     case S_IFREG:
  146.         if(x.st_nlink > 0)    /* !pipe */
  147.             return(1);
  148.         else
  149.             return(0);
  150.     case S_IFCHR:
  151.         if(isatty(fileno(f)))
  152.             return(0);
  153.         return(1);
  154. #ifdef S_IFBLK
  155.     case S_IFBLK:
  156.         return(1);
  157. #endif
  158.     }
  159. #else
  160. #ifdef S_ISDIR
  161.     /* POSIX version */
  162.     if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
  163.         if(x.st_nlink > 0)    /* !pipe */
  164.             return(1);
  165.         else
  166.             return(0);
  167.         }
  168.     if (S_ISCHR(x.st_mode)) {
  169.         if(isatty(fileno(f)))
  170.             return(0);
  171.         return(1);
  172.         }
  173.     if (S_ISBLK(x.st_mode))
  174.         return(1);
  175. #else
  176.     Help! How does fstat work on this system?
  177. #endif
  178. #endif
  179.     return(0);    /* who knows what it is? */
  180. #endif
  181. }
  182. nowreading(x) unit *x;
  183. {
  184.     long loc;
  185.     extern char *r_mode[];
  186.     if (!x->ufnm)
  187.         goto cantread;
  188.     loc=ftell(x->ufd);
  189.     if(freopen(x->ufnm,r_mode[x->ufmt],x->ufd) == NULL) {
  190.  cantread:
  191.         errno = 126;
  192.         return(1);
  193.         }
  194.     x->uwrt=0;
  195.     (void) fseek(x->ufd,loc,SEEK_SET);
  196.     return(0);
  197. }
  198. nowwriting(x) unit *x;
  199. {
  200.     long loc;
  201.     int k;
  202.     extern char *w_mode[];
  203.  
  204.     if (!x->ufnm)
  205.         goto cantwrite;
  206.     if (x->uwrt == 3) { /* just did write, rewind */
  207.         if (close(creat(x->ufnm,0666)))
  208.             goto cantwrite;
  209.         }
  210.     else {
  211.         loc=ftell(x->ufd);
  212.         if (fclose(x->ufd) < 0
  213.         || (k = x->uwrt == 2 ? creat(x->ufnm,0666)
  214.                      : open(x->ufnm,O_WRONLY)) < 0
  215.         || (cf = x->ufd = fdopen(k,w_mode[x->ufmt])) == NULL) {
  216.             x->ufd = NULL;
  217.  cantwrite:
  218.             errno = 127;
  219.             return(1);
  220.             }
  221.         (void) fseek(x->ufd,loc,SEEK_SET);
  222.         }
  223.     x->uwrt = 1;
  224.     return(0);
  225. }
  226.